package com.action.pc.ach;

import com.bean.Ach_record;
import com.bean.Sys_user;
import com.common.BaseAction;
import com.common.Cache;
import com.common.ModelAndJson;
import com.github.pagehelper.Page;
import com.service.ach.AchService;
import com.util.StringUtil;
import com.util.excel.ExcelDataFormatter;
import com.util.excel.ExcelUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 业绩
 * Created by lvjianqing on 2017/9/19.
 */
@Controller
@RequestMapping("ach")
public class AchAction extends BaseAction {
    @Autowired
    private AchService achService;

    @RequestMapping("list")
    public ModelAndView list(ModelMap modelMap, AchVo vo) {
        int pageNum = vo.getPageNumInt();
        int pageSize = vo.getPageSizeInt();
        HashMap<String, Object> paraMap = new HashMap<>();
        String achtime = vo.getAchtime();
        if (StringUtil.isEmpty(achtime)) {
            achtime = getActTime();
            vo.setAchtime(getActTime());
        }
        paraMap.put("achtime", achtime);
        filterParaMap(paraMap);
        Page page = achService.findAchListByPage(pageNum, pageSize, paraMap);
        modelMap.put("page", page);
        modelMap.put("pages", page.getPages());
        modelMap.put("pageNum", page.getPageNum());
        modelMap.put("pageSize", page.getPageSize());
        modelMap.put("vo", vo);
        return new ModelAndView("/pc/ach_list", modelMap);
    }

    @RequestMapping("mylist")
    public ModelAndView mylist(ModelMap modelMap, AchVo vo) {
        Sys_user user = getSysUser();
        int pageNum = vo.getPageNumInt();
        int pageSize = vo.getPageSizeInt();
        HashMap<String, Object> paraMap = new HashMap<>();
        String starttime = vo.getStarttime();
        String endtime = vo.getEndtime();
        paraMap.put("starttime", starttime);
        paraMap.put("endtime", endtime);
        paraMap.put("cityid", user.getCityid());
        Page page = achService.findAchListByPage(pageNum, pageSize, paraMap);
        List<Ach_record> list = page.getResult();
        if (list != null) {
            Double orderamounts = 0d;
            Double payamount = 0d;
            Double amounts = 0d;
            for (Ach_record r : list) {
                Double oa = r.getOrderamount() == null ? 0d : r.getOrderamount().doubleValue();
                Double pa = r.getPayamount() == null ? 0d : r.getPayamount().doubleValue();
                Double a = r.getAmount() == null ? 0d : r.getAmount().doubleValue();
                orderamounts += oa;
                payamount += pa;
                amounts += a;
            }
            modelMap.put("orderamounts", orderamounts);
            modelMap.put("payamount", payamount);
            modelMap.put("amounts", amounts);
        }
        modelMap.put("page", page);
        modelMap.put("pages", page.getPages());
        modelMap.put("pageNum", page.getPageNum());
        modelMap.put("pageSize", page.getPageSize());
        modelMap.put("vo", vo);
        return new ModelAndView("/pc/ach_mylist", modelMap);
    }

    private String getActTime() {
        DateTime date = new DateTime();
        int day = StringUtil.String2Integer(date.toString("dd")).intValue();
        if (day <= 15) {
            return date.plusMonths(-2).toString("yyyy-MM");
        } else {
            return date.plusMonths(-1).toString("yyyy-MM");
        }
    }

    @RequestMapping("report")
    public void report(HttpServletResponse response, AchVo vo) {
        HashMap<String, Object> paraMap = new HashMap<>();
        String achtime = vo.getAchtime();
        if (StringUtil.isEmpty(achtime)) {
            achtime = getActTime();
            vo.setAchtime(getActTime());
        }
        paraMap.put("achtime", achtime);
        Page page = achService.findAchListByPage(1, 0, paraMap);
        List<Ach_record> list = page.getResult();
        ExcelUtils<Ach_record> excelUtil = new ExcelUtils<Ach_record>(new Ach_record());
        try {
            ExcelDataFormatter edf = new ExcelDataFormatter();
            Map<String, String> map = new HashMap<String, String>();
            HashMap<String, String> cityMap = Cache.cityMap;
            Set<String> keys = cityMap.keySet();
            for (String key : keys) {
                map.put(key, cityMap.get(key));
            }
            edf.set("cityid", map);
            ExcelUtils.writeResponse(response, list, edf, "业绩报表", new Ach_record());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 按月份结算
     *
     * @param modelAndJson
     * @param vo
     * @return
     */
    @RequestMapping("close")
    @ResponseBody
    public String close(ModelAndJson modelAndJson, AchVo vo) {
        HashMap<String, Object> paraMap = new HashMap<>();
        String achtime = vo.getAchtime();
        if (StringUtil.isEmpty(achtime)) {
            achtime = getActTime();
            vo.setAchtime(getActTime());
        }
        paraMap.put("achtime", achtime);
        paraMap.put("status", (byte) 0);
        Page page = achService.findAchListByPage(1, 0, paraMap);
        if (page != null && page.getTotal() > 0) {
            achService.closeAch(achtime, (byte) 1);
        } else {
            modelAndJson.setFalse("-1", "请选择未结算的月份");
        }
        return modelAndJson.toString();
    }

}
